﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LongQuanWeb.App_Code;
using Maticsoft.Common;

namespace FeiHongWeb.Admin
{
    public partial class NewsAddUpdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((this.Session["username"] == null) || (this.Session["username"].ToString() == string.Empty))
            {
                base.Response.Write("<font color=#ff0000 style='FONT-SIZE: 12px'>对不起,您没足够权限访问此页!!</font><br>");
                base.Response.Write("<a href='Login.aspx' target=_top style='FONT-SIZE: 12px'>重新登陆</a><br>");
                base.Response.End();
            }
            if (!IsPostBack)
            {
                base.Response.Write("<script>parent.window.document.getElementById( 'lbl_title').innerHTML='活动编辑'; </script>");
                string type = Request.QueryString["type"].ToString().Trim();
                if (type == "2")
                {
                    string id = Request.QueryString["id"].ToString().Trim();
                    string strSql = "Select * From Activity Where AcitivityNum=" + id + "";
                    DataTable dt = DbHelperSQL.Query(strSql).Tables[0];
                    if (dt != null && dt.Rows.Count != 0)
                    {
                        txtContent.Value = dt.Rows[0]["ActivityDescription"].ToString();
                        txtContent2.Value = dt.Rows[0]["ActivityContent"].ToString();
                        this.txt_title.Text = dt.Rows[0]["ActivityName"].ToString();
                        this.txt_title2.Text = dt.Rows[0]["StartDate"].ToString();
                        //this.ddl_news.SelectedValue = dt.Rows[0]["ActivityType"].ToString();
                    }
                    btn_return.CommandArgument = type;
                }

            }
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            string type = btn_return.CommandArgument.ToString();
            try
            {
                DateTime time;
                if(!DateTime.TryParse(txt_title2.Text.Trim(), out time))
                {
                    MessageBox.Show(this,"时间格式不正确！");
                    return ;
                }
                if (type == "1")
                {
                    string strSql = "insert into [Activity](ActivityCode,ActivityName,ActivityDescription,ActivityType,ActivityContent,VideoPath,PicPath,StartDate,createtime,BrowerCount,Source,CurrentPeopleNum,SubCategoryType) "
                                  + "values('Activitys','" + this.txt_title.Text.Trim() + "','" + this.txtContent.Value + "','null','" + this.txtContent2.Value + "','null','null'," + time.ToShortDateString() + "," + DateTime.Now.ToShortDateString() + ",'0','null','0','1')";
                    if (DbHelperSQL.ExecuteSql(strSql) > 0)
                    {
                        base.Response.Write("<script>alert('添加成功');window.location='NewsAddUpdate.aspx?type='</script>");
                    }
                    else
                    {
                        base.Response.Write("<script>alert('添加失败');</script>");
                    }
                }
                else if (type == "2")
                {
                    string id = Request.QueryString["id"].ToString().Trim();
                    string sql = "Update [Activity] set ActivityDescription='" + this.txtContent.Value + "',ActivityContent='" + this.txtContent2.Value + "',ActivityName='" + this.txt_title.Text.Trim() + "',StartDate='" + this.txt_title2.Text.Trim() + "'" + " where AcitivityNum=" + id + "";
                    if (DbHelperSQL.ExecuteSql(sql) > 0)
                    {
                        base.Response.Write("<script>alert('修改成功');window.location='News.aspx'</script>");
                    }
                    else
                    {
                        base.Response.Write("<script>alert('修改失败');</script>");
                    }
                }
            }
            catch (Exception er)
            {
                Response.Write(" <script defer>alert('" + er.Message + "') </script>");
            }
        }

        protected void btn_return_Click(object sender, EventArgs e)
        {
            string type = btn_return.CommandArgument.ToString();
            if (type == "1")
            {
                Response.Redirect("NewsAddUpdate.aspx?type=1");
            }
            else if (type == "2")
            {
                Response.Redirect("News.aspx");
            }
        }
    }
}